﻿using KouDai.WinPad.Models;
using Newtonsoft.Json.Linq;
using NextEngine.HttpEngine;
using NextEngine.Parser;
using System.Collections.Generic;

namespace KouDai.WinPad.Parser
{
    public class SearchCategoryParser : NParser
    {
        public override void ParseData(string json, BackData backData)
        {
            List<SearchCategoryEntity> searchCategoryList = backData.ResposeData as List<SearchCategoryEntity>;
            JObject jobject = JObject.Parse(json);
            JToken jtoken = jobject.SelectToken("result");
            foreach (JToken jt in jtoken.Children())
            {
                SearchCategoryEntity searchCategoryEntity = new SearchCategoryEntity();
                searchCategoryEntity.Id = jt.Value<string>("id");
                searchCategoryEntity.ItemTagName = jt.Value<string>("item_tag_name");
                searchCategoryEntity.ImageUrl = jt.Value<string>("img_url");
                searchCategoryEntity.ParentId = jt.Value<string>("parent_id");
                searchCategoryList.Add(searchCategoryEntity);
                if (jt.ToString().Contains("childCategories"))
                {
                    List<SearchCategoryEntity> subCategories = new List<SearchCategoryEntity>();
                    JToken subJts = jt.SelectToken("childCategories");
                    foreach (JToken subJt in subJts.Children())
                    {
                        SearchCategoryEntity searchEntity = new SearchCategoryEntity();
                        searchEntity.Id = subJt.Value<string>("id");
                        searchEntity.ItemTagName = subJt.Value<string>("item_tag_name");
                        searchEntity.ImageUrl = subJt.Value<string>("img_url");
                        searchEntity.ParentId = subJt.Value<string>("parent_id");
                        subCategories.Add(searchEntity);
                    }
                    searchCategoryEntity.SubCategories = subCategories;
                }
            }
        }
    }
}
